home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / pascal / swag / textfile.swg / 0028_BLOCKREAD-WRITE Text file.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1993-11-02  |  2.6 KB  |  101 lines

  1. {
  2. JAN DOGGEN
  3.  
  4. > I have already written the parts that open and read the File and find the
  5. > Record I need to update.  Now I want to replace part of the String of
  6. > Characters which comprise this Record, With the Record remaining in its
  7. > location in the File.
  8.  
  9. No, if you use a Text File (Var T: Text) it's either read or Write.
  10.  
  11.  1. if you only replace 'n' Characters With another 'n' Characters, it
  12.  is no big problem, although hardly an elegant solution:
  13.  you can Type it as a File of Byte, then read /Write each String
  14.  using something like:
  15. }
  16.  
  17. Procedure BlockWriteStr(Var F : File; S : String);
  18. Var
  19.   L, Written : Word;
  20. begin
  21.   L := Length(S) + 1;
  22.   BlockWrite(File(F), S[0], L, Written);
  23.   Assert(L = Written, 'Error writing to disk (disk full ?)');
  24. end;
  25.  
  26.  
  27. Procedure BlockReadStr(Var F : File; Var S : String);
  28. Var
  29.   ReadIn : Word;
  30. begin
  31.   BlockRead(File(F), S[0], SizeOf(Byte));
  32.   BlockRead(File(F), S[1], Ord(S[0]), ReadIn);
  33.   Assert(Ord(S[0]) = ReadIn, 'Error reading from disk');
  34. end;
  35.  
  36. { Of course, you'll have to remember your FilePos().
  37.  
  38.  2. if you replace With a different number of Chars, I cannot help
  39.  you, other than suggesting you use an input and output Text File,
  40.  and reWrite the whole thing. Not very elegant either.
  41.  
  42.  BTW, as I am still in my editor, I might as well copy this too:
  43. }
  44.  
  45. Function SubstituteStr(Original, Part1, Part2 : String): String;
  46. (* Replaces all <Part1> subStrings in String <Original> With <Part2>.
  47.  *
  48.  * Example:
  49.  *   SubstituteStr('Abracadabra','ra','rom') ==> 'Abromcadabrom'
  50.  * The Function does not work recursively, so:
  51.  *   SubstituteStr('Daaaaaaaar','aa','a') returns 'Daaaar', not 'Dar'.*)
  52. Var
  53.   S       : String;
  54.   P, L, T : Byte;
  55. begin
  56.   if Original = '' then
  57.   begin
  58.     SubstituteStr := '';
  59.     Exit;
  60.   end;
  61.  
  62.   S := '';
  63.   L := Length(Part1);
  64.   T := 1;
  65.   P := Pos(Part1,Copy(Original,T,255));
  66.  
  67.   While P <> 0 DO
  68.   begin
  69.     S := S + Copy(Original, T, P - 1) + Part2;
  70.     T := T + P + L - 1;
  71.     P := Pos(Part1, Copy(Original, T, 255));
  72.   end;
  73.   SubstituteStr := S + Copy(Original, T, 255);
  74. end;
  75.  
  76. Function SubstituteStrX(Original, Part1, Part2 : String) : String;
  77. (* Like SubstituteStr, but now the Function works recursively, so
  78. *   SubstituteStrX('Daaaaaaaar','aa','a') returns 'Dar'. *)
  79. Var
  80.   S       : String;
  81.   P, L, T : Byte;
  82. begin
  83.   if Original = '' then
  84.   begin
  85.     SubstituteStrX := '';
  86.     Exit;
  87.   end;
  88.  
  89.   S := Original;
  90.   T := 1;
  91.   L := Length(Part1);
  92.   P := Pos(Part1,S);
  93.  
  94.   While P <> 0 DO
  95.   begin
  96.     S := Copy(S, 1, P - 1) + Part2 + Copy(S, P + L, 255);
  97.     P := Pos(Part1, S);
  98.   end;
  99.   SubstituteStrX := S;
  100. end;
  101.